package com.amap.flutter.map;

import android.content.Context;


import com.amap.flutter.map.core.AMapOptionsSink;
import com.amap.flutter.map.utils.LogUtil;
import com.shareware.common.utils.LogExtKt;
import com.t3map.api.map.model.T3CameraPosition;
import com.t3map.api.map.model.T3CustomMapStyleOptions;
import com.t3map.api.map.model.T3LatLngBounds;
import com.t3map.api.map.model.T3MyLocationStyle;


import java.util.List;

import io.flutter.plugin.common.BinaryMessenger;

/**
 * @author whm
 * @date 2020/10/29 10:13 AM
 * @mail hongming.whm@alibaba-inc.com
 * @since
 */
class AMapOptionsBuilder implements AMapOptionsSink {
    private static final String CLASS_NAME = "AMapOptionsBuilder";

    private T3CameraPosition camera;
    private int mapType;
    private T3CustomMapStyleOptions customMapStyleOptions;
    private T3MyLocationStyle myLocationStyle;

    private float minZoomLevel = 3;
    private float maxZoomLevel = 20;
    private T3LatLngBounds latLngBounds;
    private boolean trafficEnabled = true;
    private boolean touchPoiEnabled = true;
    private boolean buildingsEnabled = true;
    private boolean labelsEnabled = true;
    private boolean compassEnabled = true;
    private boolean zoomGesturesEnabled = true;
    private boolean scrollGesturesEnabled = true;
    private boolean rotateGesturesEnabled = true;
    private boolean tiltGesturesEnabled = true;
    private boolean scaleEnabled = true;
    private boolean zoomControlsEnabled = true;

    private float anchorX = 2.0F;
    private float anchorY = 2.0F;

    private Object initialMarkers;

    private Object initialPolyLines;

    private Object initialPolygons;

    AMapPlatformView build(int id, Context context, BinaryMessenger binaryMessenger,
                           LifecycleProvider lifecycleProvider) {
        try {
            final AMapPlatformView aMapPlatformView = new AMapPlatformView(id, context, binaryMessenger, lifecycleProvider);

            if (null != customMapStyleOptions) {
                aMapPlatformView.getMapController().setCustomMapStyleOptions(customMapStyleOptions);
            }

            if (null != myLocationStyle) {
                aMapPlatformView.getMapController().setMyLocationStyle(myLocationStyle);
            }
            if (anchorX >= 0 && anchorX <= 1.0 && anchorY <= 1.0 && anchorY >= 0) {
                aMapPlatformView.getMapController().setScreenAnchor(anchorX, anchorY);
            }

            aMapPlatformView.getMapController().setMinZoomLevel(minZoomLevel);
            aMapPlatformView.getMapController().setMaxZoomLevel(maxZoomLevel);

            if (null != latLngBounds) {
                aMapPlatformView.getMapController().setLatLngBounds(latLngBounds);
            }

            aMapPlatformView.getMapController().setTrafficEnabled(trafficEnabled);
            aMapPlatformView.getMapController().setTouchPoiEnabled(touchPoiEnabled);
            aMapPlatformView.getMapController().setBuildingsEnabled(buildingsEnabled);
            aMapPlatformView.getMapController().setLabelsEnabled(labelsEnabled);

            if (null != initialMarkers) {
                List<Object> markerList = (List<Object>) initialMarkers;
                aMapPlatformView.getMarkersController().addByList(markerList);
            }

            if (null != initialPolyLines) {
                List<Object> markerList = (List<Object>) initialPolyLines;
                aMapPlatformView.getPolylinesController().addByList(markerList);
            }

            if (null != initialPolygons) {
                List<Object> polygonList = (List<Object>) initialPolygons;
                aMapPlatformView.getPolygonsController().addByList(polygonList);
            }

            if (camera != null) {
                aMapPlatformView.getMapController().setCamera(camera);
            }
            aMapPlatformView.getMapController().setMapType(mapType);
            aMapPlatformView.getMapController().setCompassEnabled(compassEnabled);
            aMapPlatformView.getMapController().setZoomGesturesEnabled(zoomGesturesEnabled);
            aMapPlatformView.getMapController().setScrollGesturesEnabled(scrollGesturesEnabled);
            aMapPlatformView.getMapController().setRotateGesturesEnabled(rotateGesturesEnabled);
            aMapPlatformView.getMapController().setTiltGesturesEnabled(tiltGesturesEnabled);
            aMapPlatformView.getMapController().setScaleEnabled(scaleEnabled);
            aMapPlatformView.getMapController().setZoomControlsEnabled(zoomControlsEnabled);

            return aMapPlatformView;
        } catch (Throwable e) {
//            LogUtil.e(CLASS_NAME, "build", e);
            LogExtKt.log(CLASS_NAME, "build flutter地图发生异常了");
            LogExtKt.logStackTrace(CLASS_NAME, e);
        }
        return null;
    }

    @Override
    public void setCamera(T3CameraPosition camera) {
        this.camera = camera;
    }

    @Override
    public void setMapType(int mapType) {
        this.mapType = mapType;
    }

    @Override
    public void setCustomMapStyleOptions(T3CustomMapStyleOptions customMapStyleOptions) {
        this.customMapStyleOptions = customMapStyleOptions;
    }

    @Override
    public void setMyLocationStyle(T3MyLocationStyle myLocationStyle) {
        this.myLocationStyle = myLocationStyle;
    }

    @Override
    public void setScreenAnchor(float x, float y) {
        anchorX = x;
        anchorY = y;
    }

    @Override
    public void setMinZoomLevel(float minZoomLevel) {
        this.minZoomLevel = minZoomLevel;
    }

    @Override
    public void setMaxZoomLevel(float maxZoomLevel) {
        this.maxZoomLevel = maxZoomLevel;
    }

    @Override
    public void setLatLngBounds(T3LatLngBounds latLngBounds) {
        this.latLngBounds = latLngBounds;
    }

    @Override
    public void setTrafficEnabled(boolean trafficEnabled) {
        this.trafficEnabled = trafficEnabled;
    }

    @Override
    public void setTouchPoiEnabled(boolean touchPoiEnabled) {
        this.touchPoiEnabled = touchPoiEnabled;
    }

    @Override
    public void setBuildingsEnabled(boolean buildingsEnabled) {
        this.buildingsEnabled = buildingsEnabled;
    }

    @Override
    public void setLabelsEnabled(boolean labelsEnabled) {
        this.labelsEnabled = labelsEnabled;
    }

    @Override
    public void setCompassEnabled(boolean compassEnabled) {
        this.compassEnabled = compassEnabled;
    }

    @Override
    public void setZoomGesturesEnabled(boolean zoomGesturesEnabled) {
        this.zoomGesturesEnabled = zoomGesturesEnabled;
    }

    @Override
    public void setScrollGesturesEnabled(boolean scrollGesturesEnabled) {
        this.scrollGesturesEnabled = scrollGesturesEnabled;
    }

    @Override
    public void setRotateGesturesEnabled(boolean rotateGesturesEnabled) {
        this.rotateGesturesEnabled = rotateGesturesEnabled;
    }

    @Override
    public void setTiltGesturesEnabled(boolean tiltGesturesEnabled) {
        this.tiltGesturesEnabled = tiltGesturesEnabled;
    }

    @Override
    public void setScaleEnabled(boolean scaleEnabled) {
        this.scaleEnabled = scaleEnabled;
    }

    public void setZoomControlsEnabled(boolean zoomControlsEnabled) {
        this.zoomControlsEnabled = zoomControlsEnabled;
    }

    @Override
    public void setInitialMarkers(Object markersObject) {
        this.initialMarkers = markersObject;
    }

    @Override
    public void setInitialPolylines(Object polylinesObject) {
        this.initialPolyLines = polylinesObject;
    }

    @Override
    public void setInitialPolygons(Object polygonsObject) {
        this.initialPolygons = polygonsObject;
    }


}
